package view;

import com.alee.extended.painter.BorderPainter;
import com.alee.extended.painter.NinePatchIconPainter;
import com.alee.extended.panel.WebOverlay;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.text.WebTextArea;
import com.alee.utils.swing.ConditionalVisibilityListener;
import controller.Comment;
import model.PostModel;
import util.FontLoader;
import util.ImageLoader;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.text.SimpleDateFormat;

public class PostView extends WebPanel {
    private static final long serialVersionUID = -3828631441418570187L;
    private static final SimpleDateFormat formatter = new SimpleDateFormat("h:mm a - MMM d, yyyy");

    public WebLabel authorView, dateView;
    public WebTextArea textView, commentField;
    public WebPanel commentPanel;
    public PostModel model;

    /**
     * Construct a post view for a model.
     * @param model
     */
    public PostView(PostModel model) {
        NinePatchIconPainter ninePatchPainter = new NinePatchIconPainter(ImageLoader.getImage("post.9.png"));
        
        this.model = model;
        this.setPainter(ninePatchPainter);
        this.setMargin(0, 0, 40, 0);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        
        authorView = this.createAuthor();
        dateView = this.createDate();
        
        WebOverlay authorDatePanel = new WebOverlay();
        authorDatePanel.setComponent(authorView);
        authorDatePanel.setOpaque(false);
        authorDatePanel.addOverlay(dateView, SwingConstants.RIGHT, SwingConstants.CENTER);
        authorDatePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        this.add(authorDatePanel);
        
        textView = this.createText();
        this.add(textView);
        this.createComments();
    }
    
    /**
     * Create the author of the post
     * @return Label of author
     */
    public WebLabel createAuthor() {
        WebLabel authorLabel = new WebLabel(model.authorName, ImageLoader.getImage("kanye.png"));
        Font osBold = FontLoader.getFont(FontLoader.OPENSANS_BOLD);
        authorLabel.setFont(osBold.deriveFont(14f));
        authorLabel.setForeground(new Color(243, 117, 102));
        authorLabel.setMargin(2, 8, 0, 8);
        return authorLabel;
    }
    
    
    /**
     * Create the text of the post.
     * @return Text area
     */
    public WebTextArea createText() {
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        WebTextArea text = new WebTextArea(model.text);
        text.setEditable(false);
        text.setCursor(null);
        text.setOpaque(false);
        text.setFocusable(false);
        text.setWrapStyleWord(true);
        text.setLineWrap(true);
        text.setFont(osRegular.deriveFont(12f));
        text.setForeground(new Color(60, 60, 60));
        text.setAlignmentX(Component.LEFT_ALIGNMENT);
        text.setMargin(0, 54, 15, 8);
        return text;
    }
    
    
    /**
     * Create the date 
     * @return Date label
     */
    public WebLabel createDate() {
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        WebLabel timeLabel = new WebLabel(formatter.format(model.date));
        timeLabel.setFont(osRegular.deriveFont(12f));
        timeLabel.setForeground(new Color(140, 140, 140));
        return timeLabel;
    }
    
    

    /**
     * Create the comment field. Adds itself.
     * @return Comment field
     */
    public WebTextArea createCommentField(JComponent parent) {
        final WebTextArea textField = new WebTextArea("");
        BorderPainter bp2 = new BorderPainter ();
        
        bp2.setMargin(3);
        bp2.setRound ( 4 );
        bp2.setWidth ( 1 );
        bp2.setColor ( new Color ( 200, 200, 200 ) );
        textField.setPainter ( bp2 );
        
        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        textField.setFont(osRegular.deriveFont(12f));
        textField.setAlignmentX(Component.LEFT_ALIGNMENT);
        textField.setWrapStyleWord(true);
        textField.setLineWrap(true);
        textField.setForeground(new Color(100, 100, 100));

        // Overlayed text field
        WebOverlay overlayPanel = new WebOverlay();
        overlayPanel.setComponent(textField);

        // Label displayed as overlay when component is not focused
        @SuppressWarnings("serial")
        final WebLabel overlay = new WebLabel("Write a comment...") {
            public boolean contains(int x, int y) {
                return false;
            }
        };
        
        overlay.setFont(osRegular.deriveFont(12f));
        overlay.setForeground(Color.GRAY);
        overlay.setMargin(textField.getInsets());
        
        textField.addFocusListener(new ConditionalVisibilityListener(overlay, false, null) {
            public boolean isVisible() {
                return super.isVisible() && textField.getText().equals("");
            }
        });
        
        textField.getDocument().putProperty("filterNewlines", Boolean.TRUE);
        
        overlayPanel.addOverlay(overlay, SwingConstants.LEFT, SwingConstants.CENTER);
        overlayPanel.setOpaque(false);
        overlayPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        parent.add(overlayPanel);
        
        return textField;
    }
    
    /**
     * Append a new comment to the view.
     * @param comment Comment controller
     */
    public void appendComment(Comment comment) {
        this.commentPanel.add(comment.view , this.commentPanel.getComponentCount() - 2);
    }
    

    /**
     * Create the views for all the comments.
     */
    public void createComments() {
        commentPanel = new WebPanel();
        commentPanel.setLayout(new BoxLayout(commentPanel, BoxLayout.Y_AXIS));
        for (Comment c : this.model.comments) {
            commentPanel.add(c.view);
        }
        commentPanel.setMargin(0, 54, 10, 0);
        commentPanel.setOpaque(false);
        
        JLabel spacer = new JLabel();
        spacer.setPreferredSize(new Dimension(10, 15));
        commentPanel.add(spacer);
        
        commentField = this.createCommentField(commentPanel);
        this.add(commentPanel);
    }
}
